package com.ss.android.video;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

public class CircleView extends ImageView {
    private Paint mPaint;
    private int mColor;
    private float mStrokeWidth;
    private int mCurrentProgress;
    private int mMax;
    private boolean isStop;
    private RectF mBounds;

    public CircleView(Context context) {
        this(context, null);
    }

    public CircleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleView(Context context, AttributeSet attrs, int defaultStyle) {
        super(context, attrs, defaultStyle);
        this.isStop = false;
        this.mBounds = new RectF();
        this.mPaint = new Paint();
        this.mColor = -1;
        this.mStrokeWidth = 2f;
        this.mMax = 5000;
        this.mCurrentProgress = 0;
    }

    public synchronized boolean getIsStop() {
        return this.isStop;
    }

    public synchronized void setIsStop(boolean isStop) {
        this.isStop = isStop;
    }

    public synchronized int getMax() {
        return this.mMax;
    }

    public synchronized void setMax(int max) {
        if (max >= 0) {
            this.mMax = max;
        }
        throw new IllegalArgumentException("max not less than 0");
    }

    public synchronized int getProgress() {
        return this.mCurrentProgress;
    }

    public synchronized void setProgress(int progress) {
        if (progress >= 0) {
            if (progress > this.mMax) {
                progress = this.mMax;
            }
            if (progress <= this.mMax) {
                this.mCurrentProgress = progress;
                if (this.isStop) {
                    this.mCurrentProgress = 0;
                }
                this.postInvalidate();
            }
        }
        throw new IllegalArgumentException("less than 0");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int halfWidth = this.getWidth() / 2;
        int radius = (int) ((float) halfWidth - this.mStrokeWidth / 2f);
        this.mPaint.setStrokeWidth(this.mStrokeWidth);
        this.mPaint.setColor(this.mColor);
        this.mBounds.left = (float) (halfWidth - radius);
        this.mBounds.top = (float) (halfWidth - radius);
        this.mBounds.right = (float) (halfWidth + radius);
        this.mBounds.bottom = (float) (halfWidth + radius);
        this.mPaint.setStyle(Paint.Style.STROKE);
        this.mPaint.setAntiAlias(true);
        canvas.drawArc(
                this.mBounds,
                -90f,
                (float) (this.mCurrentProgress * 360 / this.mMax),
                false,
                this.mPaint);
    }
}

